#!/bin/sh
#=======================================================================
# run_30yr.sh
#   Run the SPEEDY model for 30 years starting from the state of rest.
#                                            2012/11/03 Daisuke Hotta
#=======================================================================
set -e
#-----------------------------------------------------------------------
# Modify below according to your environment
#-----------------------------------------------------------------------
compiler=pgi    # pgi or gfortran
schemename='ncyc3' # name of the time-integration scheme
# version of N-cycle, choose one from the following:
#    _3a, _3b, _3ab, _4a, _4b, _4abba
# for Leapfrog, set this to '' (null string)
ncycversion='_4abba' 
cd ../..
SPEEDY=`pwd`
# directory for output data
OUTPUT=${SPEEDY}/DATA/nature_${schemename}${ncycversion}
# work directory
TMPDIR=${SPEEDY}/model/wk_exec/${schemename}${ncycversion}
#-----------------------------------------------------------------------
# Usually do not modify below
#-----------------------------------------------------------------------
mkdir -p $OUTPUT
cp $SPEEDY/common/yyyymmddhh.ctl $OUTPUT
cp $SPEEDY/common/yyyymmddhh_p.ctl $OUTPUT
#
# Work directory
#
rm -rf $TMPDIR
mkdir -p $TMPDIR
cd $TMPDIR
#
# 30-year run
#
### date setting
IYYYY=1981
IMM=01
IDD=01
IHH=00
### build
echo '>>>BEGIN BUILDING SPEEDY MODEL'
cp $SPEEDY/model/source/makefile .
cp $SPEEDY/model/source/*.h .
cp $SPEEDY/model/source/*.f .
cp $SPEEDY/model/source/*.s .

mv par_horres_t30.h atparam.h
mv par_verres.h atparam1.h

cp $SPEEDY/model/ver32.input/cls_*.h .
cp $SPEEDY/model/ver32.input/inpfiles.s .

cp $SPEEDY/model/update/*.h .
cp $SPEEDY/model/update/*.f .
cp $SPEEDY/model/update/makefile .

## scheme specific
cp $SPEEDY/model/update2/${schemename}/*.f .
cp $SPEEDY/model/update2/${schemename}/*.h .
cp $SPEEDY/model/update2/${schemename}/cls_instep.h_30yr ./cls_instep.h
if [[ $schemename == *ncyc* ]]; then
  cp $SPEEDY/model/update2/ncyc_parm/ncycparm.h${ncycversion} ./ncycparm.h
fi
cp $SPEEDY/model/update2/${schemename}/makefile_${compiler} ./makefile

make imp.exe

sed -e 's/=../=..\/../g' <inpfiles.s>inpfiles2.s
sh inpfiles2.s t30

echo '>>>END BUILDING SPEEDY MODEL'
### run
FORT2=0
echo ">>>BEGIN 30-YEAR COMPUTATION OF $IYYYY/$IMM/$IDD/$IHH"
echo $FORT2 > fort.2
echo $IYYYY >> fort.2
echo $IMM >> fort.2
echo $IDD >> fort.2
echo $IHH >> fort.2
time ./imp.exe > out.lis1
mv fort.10 restart.dat
### clean up
#rm -f *.o
#rm -f imp.exe

echo "NORMAL END"

